package com.tedu.egmvc.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.tedu.egmvc.mapper.AddressMapper;
import com.tedu.egmvc.pojo.Address;
import com.tedu.egmvc.pojo.AddressDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @RequestMapping("/address/insert")
    @ResponseBody
    public String insert(AddressDto addressDto) {
        Address address = new Address();
        System.out.println(address);
        BeanUtils.copyProperties(addressDto, address);
        System.out.println(address);
        addressMapper.insert(address);
        return "添加成功";
    }

    @RequestMapping("/address/select")
    @ResponseBody
    public List<Address> findAll() {
        List<Address> list = addressMapper.findAll();
        return list;
    }
    @RequestMapping("/address/delete")
    @ResponseBody
    public String delete(int id) {
        int num=addressMapper.delete(id);
        return num>0?"删除成功":"删除失败";
    }
    @RequestMapping("/address/update")
    @ResponseBody
    public String update(Address address){
//        System.out.println(address);
//        BeanUtils.copyProperties(address, address);
//        System.out.println(address);
        int num = addressMapper.update(address);
        return num>0?"修改成功":"修改失败";
    }
}
